package com.eastmountain.chinese.util;

import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class FormatPinYin {

    public FormatPinYin() {

    }

    public static String addToneMark( String syllable ) {

        Pattern pattern = null;
        Matcher match = null;

        String toneMarkedSyllable;

        int numberedToneMark_5_Index;

        //System.out.println( "****** addToneMark" );

        toneMarkedSyllable = syllable.toLowerCase();
        toneMarkedSyllable.trim();

        for ( String[] element : matchAndRelace ) {

            pattern = Pattern.compile( element[MATCH] );
            match = pattern.matcher( toneMarkedSyllable );

            if ( match.find() ) {

                toneMarkedSyllable = match
                                .replaceFirst( element[REPLACE] );

                //System.out.printf( "%d - %s - %s\n", i,
                //                   matchAndRelace[i][MATCH],
                //                   matchAndRelace[i][REPLACE] );

                break;

            }

        }

        // Deal with the HanZi DB's use of a 5 for the neutral tone

        numberedToneMark_5_Index  = toneMarkedSyllable.indexOf( "5" );

        if ( ( numberedToneMark_5_Index = syllable.indexOf( "5" ) ) >=0 ) {

            toneMarkedSyllable = toneMarkedSyllable.substring( 0, numberedToneMark_5_Index );

        }

        return ( toneMarkedSyllable );

    }

    public static Vector<String> addToneMarks( String numberedPinYin ) {

        String toneMarkedWord;

        Vector<String> syllables = new Vector<String>();

        toneMarkedWord = numberedPinYin.toLowerCase();
        toneMarkedWord.trim();

        Vector<String> words = breakIntoWords( numberedPinYin );

        for ( String word : words ) {

            toneMarkedWord = addToneMark( word );

            syllables.add( toneMarkedWord );

        }

        return( syllables );

    }

    public static Vector<String> addToneMarksToWord( String word ) {
        
        String toneMarkedWord = "";
        String match = "";
        int offsetAfterMatch = 0;
        int charactersMatched = 0;
        
        Pattern pattern = null;
        Matcher matcher = null;
        Vector<String> characterVector = new Vector<String>();

        toneMarkedWord = word.toLowerCase();
        toneMarkedWord.trim();

        pattern = Pattern.compile( "[a-z]*[0-9]" );
        matcher = pattern.matcher( toneMarkedWord );
        
        while ( matcher.find() ) {
            
            offsetAfterMatch = matcher.end();
            
            match = matcher.group();
            
            charactersMatched = charactersMatched + match.length();
            
            System.out.println( "\n++++++++++ " + match
                              + "\n++++++++ "
                              + charactersMatched 
                              + "  -  " 
                              + word.length());
            
            characterVector.add( addToneMark( match ) );
            
        }
        
        if ( offsetAfterMatch < word.length() ) {
            
            try {
                   
                characterVector.add( addToneMark( word.substring( offsetAfterMatch ) ) );
                
             } catch ( Exception e ) {
                 
                System.out.println( "\n********** " + match
                                  + "\n********** "
                                  + charactersMatched 
                                  + "  -  " 
                                  + word.length());
             }
            
        }
        
        System.out.println( "\n  ==== characterVector: "
                            + characterVector.toString() );

        return ( characterVector );

    }

    public static Vector<String> breakIntoWords( String phrase ) {

        Pattern pattern = null;
        String[] words;
        Vector<String> wordVector = new Vector<String>();

        pattern = Pattern.compile( " " );
        words = pattern.split( phrase );

        for ( String word : words ) {

            wordVector.add( word );

        }

        return ( wordVector );

    }

    private static String MATCH_ENTRY = "[\\-.,+()_'\"a-zA-Z0-9]+";

    private static String a1 = "\u0101"; /* a1 - 257 */

    private static String a2 = "\u00e1"; /* a2 - 225 */

    private static String a3 = "\u01ce"; /* a3 - 462 */

    private static String a4 = "\u00e0"; /* a4 - 224 */

    private static String e1 = "\u0113"; /* e1 - 275 */

    private static String e2 = "\u00e9"; /* e2 - 233 */

    private static String e3 = "\u011b"; /* e3 - 283 */

    private static String e4 = "\u00e8"; /* e4 - 232 */

    private static String i1 = "\u012b"; /* i1 - 299 */

    private static String i2 = "\u00ed"; /* i2 - 237 */

    private static String i3 = "\u01d0"; /* i3 - 464 */

    private static String i4 = "\u00ec"; /* i4 - 236 */

    private static String o1 = "\u014d"; /* o1 - 333 */

    private static String o2 = "\u00f3"; /* o2 - 243 */

    private static String o3 = "\u01d2"; /* o3 - 466 */

    private static String o4 = "\u00f2"; /* o4 - 242 */

    private static String u1 = "\u016b"; /* u1 - 363 */

    private static String u2 = "\u00fa"; /* u2 - 250 */

    private static String u3 = "\u01d4"; /* u3 - 468 */

    private static String u4 = "\u00f9"; /* u4 - 249 */

    private static int MATCH = 0;

    private static int REPLACE = 1;

    private static String[][] matchAndRelace = {
                    { "ang1", a1 + "ng" },
                    { "ang2", a2 + "ng" },
                    { "ang3", a3 + "ng" },
                    { "ang4", a4 + "ng" },
                    { "eng1", e1 + "ng" },
                    { "eng2", e2 + "ng" },
                    { "eng3", e3 + "ng" },
                    { "eng4", e4 + "ng" },
                    { "ing1", i1 + "ng" },
                    { "ing2", i2 + "ng" },
                    { "ing3", i3 + "ng" },
                    { "ing4", i4 + "ng" },
                    { "ong1", o1 + "ng" },
                    { "ong2", o2 + "ng" },
                    { "ong3", o3 + "ng" },
                    { "ong4", o4 + "ng" },
                    { "an1", a1 + "n" },
                    { "an2", a2 + "n" },
                    { "an3", a3 + "n" },
                    { "an4", a4 + "n" },
                    { "en1", e1 + "n" },
                    { "en2", e2 + "n" },
                    { "en3", e3 + "n" },
                    { "en4", e4 + "n" },
                    { "in1", i1 + "n" },
                    { "in2", i2 + "n" },
                    { "in3", i3 + "n" },
                    { "in4", i4 + "n" },
                    { "un1", u1 + "n" },
                    { "un2", u2 + "n" },
                    { "un3", u3 + "n" },
                    { "un4", u4 + "n" },
                    { "ao1", a1 + "o" },
                    { "ao2", a2 + "o" },
                    { "ao3", a3 + "o" },
                    { "ao4", a4 + "o" },
                    { "ou1", o1 + "u" },
                    { "ou2", o2 + "u" },
                    { "ou3", o3 + "u" },
                    { "ou4", o4 + "u" },
                    { "ai1", a1 + "i" },
                    { "ai2", a2 + "i" },
                    { "ai3", a3 + "i" },
                    { "ai4", a4 + "i" },
                    { "ei1", e1 + "i" },
                    { "ei2", e2 + "i" },
                    { "ei3", e3 + "i" },
                    { "ei4", e4 + "i" },
                    { "a1", a1 },
                    { "a2", a2 },
                    { "a3", a3 },
                    { "a4", a4 },
                    { "a1", a1 },
                    { "a2", a2 },
                    { "a3", a3 },
                    { "a4", a4 },
                    { "er2", e2 + "r" },
                    { "er3", e3 + "r" },
                    { "er4", e4 + "r" },
                    { "e1", e1 },
                    { "e2", e2 },
                    { "e3", e3 },
                    { "e4", e4 },
                    { "o1", o1 },
                    { "o2", o2 },
                    { "o3", o3 },
                    { "o4", o4 },
                    { "i1", i1 },
                    { "i2", i2 },
                    { "i3", i3 },
                    { "i4", i4 },
                    { "u1", u1 },
                    { "u2", u2 },
                    { "u3", u3 },
                    { "u4", u4 } };

}
